<?php

session_start();

require '../classes/imageClass.php';
require '../classes/albumClass.php';
require'../classes/connectionFactoryClass.php';
require'../classes/imageDao.php';
require'../classes/albumDao.php';
require'../classes/validates.php';
require'checksDate.php';

$album = new AlbumClass;
$image = new ImageClass();
$date = new checksDate();
$c = new ConnectionFactory();
$db = $c->getConnection();
$imageDao = new imageDao($db);
$albumDao = new albumDao($db);
$valid = new validates();



$directory = 'uploads/';
$directoryTmp = '../temp/';
$user=$_SESSION['user'];

$filePhoto = array();
$filePhotoType = array();
$filePhotoLegend = array();


$filePhotoName = $_POST['fotoname'];
$filePhotoType = $_POST['fototype'];
$filePhotoLegend = $_POST['legend'];
$albumName = $_POST['album'];
$photoCapa = $_POST['capa'];


//verifica se a foto da capa foi selecionada
if (!empty($photoCapa)) {

    $album->setNameAlbum($albumName);
    $album->setPhotoCapa($photoCapa);
    $album->setDirectory($directory);
    $album->setDateInclusionAlbum($date->dataAtual());
    $album->setIdUser($_SESSION['userId']);
    
    //Salva o album no banco
    $result = $albumDao->create($album);

    $album->setPhotoCapa($photoCapa);

    $re = $albumDao->getIdAlbumByName($photoCapa);

    $image->setAlbumId($re);

//Cria o diretorio com o nome do album  
    if(!file_exists('../' . $directory .$user)){
    mkdir('../' . $directory .$user);}
    if(!file_exists('../' . $directory .'/'.$user.'/' . $albumName)){
    mkdir('../' . $directory .'/'.$user.'/' . $albumName);}
    
    for ($i = 0; $i < count($filePhotoName); $i++) {
        $image->setName_photo($filePhotoName[$i]);
        $image->setType($filePhotoType[$i]);
        $image->setLegend($filePhotoLegend[$i]);


        $tmpFilePath = $directoryTmp . $filePhotoName[$i];
        $image->setDateInclusionPhoto($date->dataAtual());

        //verifica se foto existe;
        if ($tmpFilePath != "") {

            $newFilePath = '../' . $directory .'/'.$user.'/'. $albumName . '/' . $image->getName_photo();
            //Copia a foto para o album
            if (copy($tmpFilePath, $newFilePath)) {
                unlink($tmpFilePath);
                //grava os dados da foto no banco
                $imageDao->create($image);
            }
            else
                echo"<script> alert('O arquivo .$image->getName_photo(). Não foi possível fazer upload'); 
          window.location='../upload.php';</script>";
        } else {
            echo "<script> alert('Selecione um arquivo'); 
          window.location='../upload.php';</script>";
        }
    }
} else {
    echo "<script> alert('Selecione uma foto para capa'); 
          
          window.location='JavaScript: window.history.back()';</script>";
}
?>





<script>



    //<meta http-equiv="content-type" content="text/html;charset=utf-8" />;
    var result = <?php echo $result; ?>;


    if (result) {

        alert("Upload realizado com sucesso!");
        setTimeout("document.location = '../index.php'", 500);

    } else {

        alert("Não Foi Possível Realizar o upload!");
        setTimeout("document.location = '../index.php'", 500);
    }


</script>
